package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerPageDTO;
import com.qk.entity.Business;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {



    @Override
    public void updateCustomer(Customer customer) {
        if (BeanUtil.hasNullField(customer, "gender", "age", "wechat", "qq", "degree", "jobstatus", "subject", "courseId","businessId","createTime","updateTime")) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);

    }


    @Override
    public void addCoustomer(CustomerDTO dto) {
        if (BeanUtil.hasNullField(dto, "gender", "age", "wechat", "qq", "degree", "jobstatus", "subject", "courseId")) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }


    @Override
    public PageResult<CustomerPageVO> getCustomerPageList(CustomerPageDTO dto) {

        Page<CustomerPageVO> list = this.baseMapper.selectCustomerPageList(new Page<>(dto.getPage(), dto.getPageSize()), dto);

        return PageResult.<CustomerPageVO>builder()
                .total(list.getTotal())
                .rows(list.getResult())
                .build();
    }

}
